#include "adp_env.h"
#include "bconn.h"
#include "nstar_ts_pack.h"
#include "encode.h"
#include "user_cfg.h"

#define MP3_LEN 1024   
#define BLICK_T (100) //阻塞100ms
#define BLICK_S (10) //*10次，对应BLICK_T 100ms
#define DBG printf

 void nstar_encoder_start(void) {
    encoder_msg_start();
 }
 void nstar_encoder_end(void){
    encoder_msg_stop();
 }

int nstar_get_encode_data(unsigned char *p_dta, unsigned int len) {
    return encoder_pull_data(p_dta, len);
 }
 const char* visit_encoder_sta(void) {
    return NULL;
 }
 unsigned char nstar_encode_fifodep(void) {
    return 0;
 }


 unsigned char nstar_clear_encode_data() {
    encoder_clear_cache();
    return 0;
 }
 
 unsigned char nstar_encode_probe()  {
    return encoder_probe(MP3_LEN);
 }


#define NEVIF_CMD_PGNUM  7199   /*节目号 syscfg_get_serid*/
#define NEVIF_PG_TOUT  30   /*节目超时*/
#define NEVIF_PG_IP   p_readcfg->adp.pg_ip
#define NEVIF_PG_PORT p_readcfg->adp.pg_port


#define SPEED_CTL 1
#ifdef SPEED_CTL
static unsigned short speed_ctlCnt = 0;
static unsigned short __speed_ctl_get_tp(void)
{
    /*单位是秒，我们用的间隔是500ms一次超时，因此乘2*/
    unsigned short n= NEVIF_PG_TOUT*BLICK_S;
    if(n == 0)
        n = 30;
    return n;
}
static void _speed_ctl_reset(void)
{
    /*重置时，假设网络糟糕，重载的值也除以2*/
    unsigned short n = __speed_ctl_get_tp()/2;
    speed_ctlCnt = n;
}

static void _speed_ctl_inc(void)
{
    /*顺利发送时+*/
    unsigned short cnt = speed_ctlCnt;
    unsigned short time_out= __speed_ctl_get_tp();
	if(cnt >= time_out) {
		printf("pg restart\n");
		adp_pgNet_restart();
		speed_ctlCnt = 0;
	}
    
    else{
        speed_ctlCnt++;
        printf("pg send time out %d / %d\n", cnt, time_out);
    }
}

static void _speed_ctl_dec(void)
{
    /*超时-*/
    if(speed_ctlCnt > 4){
        speed_ctlCnt-=4;
    }
    else{
        speed_ctlCnt=0;
    }
}
#else
static void _speed_ctl_reset(void){};
static void _speed_ctl_inc(void){}; 
static void _speed_ctl_dec(void){};
#endif



/*发送阻塞10ms*/

static struct bsockt adp_pgNet_handle = \
BCONN_INIT_STRUCTURE("adp pg", 0, 0, 0, BLICK_T); 

static void _pgRec(struct bsockt *b)
{
    unsigned char data[512]; unsigned short len = 512;
    if(PEND_T_R()){
        if(bconn_rec(b, data, len)) {
            printf("%s rec %d bytes\n", b->name, len);
        }
        SLEPP_T_R(2);  
    }
}

static int _pgSend_passwd(int fd)
{
    unsigned char buf[20];
    unsigned short sid = NEVIF_CMD_PGNUM;
    unsigned char type = (sid == 0 ? PROID_AUTO: PROID_MANUAL);
    unsigned char len = nstar_pack_tcp_passwd(buf, 20, type);
    int wlen = write(fd, buf, len);
    if(wlen <=0 ){
        printf("pg send passwd err\n");
        adp_pgNet_restart();
    }else {
        nstar_encoder_start(); //网络连接成功才开始录音?
        _speed_ctl_reset();    //清除阻塞断开控制
    }
    return wlen;
}

static unsigned char _block_send(struct bsockt *b)
{
	unsigned char ret=0;
	unsigned char data[MP3_LEN];
    int dlen, wlen;
	int	nsel;

    dlen = nstar_get_encode_data(data, MP3_LEN);
    if(dlen != MP3_LEN) {
        printf("pg get mp3 data len err\n");
        return 0;
    }
    wlen = bconn_send(b, data, dlen);
    if(wlen == dlen) {
        _speed_ctl_dec();
        ("adp pg send %d\n", wlen);
        ret = 1;
    }else {
         printf("adp pg send wrong %d/%d\n", wlen, dlen);
        _speed_ctl_inc();
    }
    return ret;
}


static void _pgSend(struct bsockt *b)
{
	unsigned char cnt=5;
	unsigned char ret=0;
	do{
        if(nstar_encode_probe() == 0){
            break;
        }
		ret= _block_send(b);
        cnt--;
	}while(cnt > 0 && ret);
    if(cnt == 5) {
         usleep(100*1000);
    }
}



void adp_pgNet_start(void) {
    printf("adp pg start %x %d\n", NEVIF_PG_IP, NEVIF_PG_PORT);
    bconn_connect(&adp_pgNet_handle, NEVIF_PG_IP, NEVIF_PG_PORT);
}

void adp_pgNet_stop(void)
{
    
    bconn_stop(&adp_pgNet_handle);
    nstar_encoder_end();
}

void adp_pgNet_restart(void)
{
    bconn_restart(&adp_pgNet_handle);
}

void *adp_pg_task(void *parm)
{
    struct bsockt *b = &adp_pgNet_handle;
    //adp_pgNet_start();
    bconn_reg_LoadFuc(b, _pgSend_passwd);
	while(1)
	{
		bconn_machine(b);
        if(bconn_isConnect(b)) {
            _pgRec(b);
            _pgSend(b);
        }
        usleep(10*1000);
	}
	return 0;
}


unsigned char adp_pgNet_sta(void)
{
    struct bsockt *b = &adp_pgNet_handle;
    return bconn_isConnect(b);
}

unsigned char adp_pgNet_stastr(void)
{
    const char* str[] = {
        "未连接",
        "正常" 
    };
    unsigned char sel = adp_cmdNet_sta();
    sel = MIN(1, sel);
    return str[sel];
}

unsigned int adp_pgNet_speed(void)
{
    /*发送速率*/
    struct bsockt *b = &adp_pgNet_handle;
    return bconn_speed_s(b);
}




const char* visit_adpPg_net_speed(void)
{
extern unsigned char nstar_encode_fifodep(void);

	static char buf[55];
	int idx=0;
	if(0 == adp_pgNet_sta()){
		sprintf(buf, "待机中");
	}
	else{
		unsigned int speed= adp_pgNet_speed();
		idx= sprintf(buf, "已连接:");
		if(speed <= 1024)
			idx+= snprintf(&buf[idx], 55-idx, "%dB/s, 缓冲:%d%%", speed ,nstar_encode_fifodep() );
		else 
			idx+= snprintf(&buf[idx], 55-idx, "%dKB/s, 缓冲:%d%%", speed/1024 ,nstar_encode_fifodep() );
	}
	return buf;
}

unsigned int visit_adpPg_net_speed2(void)
{
    return adp_pgNet_speed();
}



void adp_encode_start(void) {
    adp_pgNet_start();
}

void adp_encode_stop(void) {
    adp_pgNet_stop();
}




